﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using static DFGK_WMS.IRepository.ITTransferBinOrderDetailRepository;
using static Dm.parser.LVal;

namespace DFGK_WMS.Repository
{
    public class TTransferBinOrderDetailRepository : BaseRepository<TTransferBinOrderDetail>, ITTransferBinOrderDetailRepository
    {
        public TTransferBinOrderDetailRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 添加移库单明细
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool AddTransferBinOrderDetails(TTransferBinOrderDetail obj)
        {
            var trans = Context.TTransferBinOrders.FirstOrDefault(r=>r.Id==obj.OrderId);
            if (trans == null)
            {
                return false;
            }
            obj.CreateUserId = trans.CreateUserId;
            obj.CreateUserName = obj.CreateUserName;
            obj.CreateDate = DateTime.Now;
            
            Context.TTransferBinOrderDetails.Add(obj);
            return Context.SaveChanges() > 0;
        }

        /// <summary>
        /// 根据id删除移库单明细
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        public bool DelTransferBinOrderDetails(int tid)
        {
            var obj = Context.TTransferBinOrderDetails.Find(tid);
            if (obj != null)
            {
                Context.TTransferBinOrderDetails.Remove(obj);
            }
            return Context.SaveChanges() > 0;
        }
        /// <summary>
        /// 显示仓位编码信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<DtoTCell> GetCells()
        {
            var list = Context.TCells.ToList();

            var dtoList = list.Select(cell => new DtoTCell
            {
                Id = cell.Id,
                Code = cell.Code
            }).ToList();

            return dtoList;
        }

        /// <summary>
        /// 递归分类物料信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<MaterialCategory> GetMaterialHierarchy(int? typeId)
        {
            var materialCategories = typeId != null ?
         Context.TMaterial.Where(m => m.TypeId == typeId).ToList() :
         Context.TMaterial.Distinct().ToList();

            var result = new List<MaterialCategory>();

            foreach (var category in materialCategories)
            {
                var materialCategory = new MaterialCategory
                {
                    CategoryId = category.TypeId,
                    CategoryName = category.TypeName,
                    Materials = new List<MaterialInfo>()
                };

                var materials = Context.TMaterial.Where(t => t.TypeId == category.TypeId).Distinct().ToList();
                foreach (var material in materials)
                {
                    materialCategory.Materials.Add(new MaterialInfo
                    {
                        Id = material.Id,
                        Name = material.Name
                    });
                }

                result.Add(materialCategory);
            }

            return result;
        }
        /// <summary>
        /// 查询物料明细信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<TMaterial> GetMaterials(string? name)
        {
           var list= Context.TMaterial.ToList();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(t => t.Name.Contains(name)).ToList();
            }
            return list; 
        }
        /// <summary>
        /// 根据物料分类查物料
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<TMaterial> GetParentTMaterial(int id)
        {
            var list=Context.TMaterial.Where(t=>t.Id==id).ToList();
            return list;

        }

        /// <summary>
        /// 查询移库单明细信息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TTransferBinOrderDetail> GetTransferBinOrderDetailsList(int orderId, int pageIndex, int pageSize, out int totalCount)
        {
            var list = Context.TTransferBinOrderDetails.AsNoTracking().ToList();
            totalCount = list.Count();
            if (orderId == null)
            {
                return list;
            }
            return list.Where(t => t.OrderId == orderId).ToList();
        }

        /// <summary>
        /// 根据id修改移库单明细
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool UpdateTransferBinOrderDetails(TTransferBinOrderDetail t)
        {
            var trans = Context.TTransferBinOrders.FirstOrDefault(r => r.Id == t.OrderId);
            if (trans == null)
            {
                return false;
            }
            t.ModifyUserId = trans.ModifyUserId;
            t.ModifyUserName = trans.ModifyUserName;
            t.ModifyDate = DateTime.Now;

            Context.TTransferBinOrderDetails.Update(t);
            return Context.SaveChanges() > 0;
        }
        
    }
}
